package com.scpii.universal.pns.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.ClientMoreDto;
import com.scpii.universal.pns.dao.CommentsDao;
import com.scpii.universal.pns.dao.UserActionsDao;
import com.scpii.universal.pns.dao.UsersDao;
import com.scpii.universal.pns.entity.Comments;
import com.scpii.universal.pns.entity.Users;
import com.scpii.universal.service.MoreContentService;

@Service
public class CircleServiceImpl extends PnsBaseServiceImpl<Comments, Long>
		implements CircleService {
	@Resource
	private CommentsDao commentsDao;
	@Resource
	private UsersDao usersDao;
	@Resource
	private UserActionsDao userActionsDao;

	@Resource
	public void setBaseDao(CommentsDao commentsDao) {
		super.setBaseDao(commentsDao);
	}

	@Resource
	private MoreContentService moreContentService;

	public List<Comments> getCircleInit(Integer appId, Long targetId,
			Integer pageNo, Integer pageSize) {
		// List<ClientMoreDto> clentmoreList = moreContentService
		// .listMoreContentByCon(appId, GenericConstants.CIRCLE_MORE_TYPE);
		List<Comments> list = null;
		// StringBuffer condTip = new StringBuffer("(");
		// String condTipStr = "";
		// if (clentmoreList != null && clentmoreList.size() > 0) {
		Long tarId = targetId;
		if (!targetId.toString().startsWith("48")) {
			tarId = 400000000000L + targetId;
		}
		// for (ClientMoreDto moreDto : clentmoreList) {
		// condTip.append(targetId + moreDto.getINT_MORE_CONTENT() + ",");
		// }
		// condTipStr = condTip.substring(0, condTip.length() - 1);
		// condTipStr = condTipStr + ")";
		StringBuffer hql = new StringBuffer(
				"select new Comments(t.id,t.content,t.createDt,t.parentId, t.createBy,u.userAilas,t.targetId) from  com.scpii.universal.pns.entity.Comments t, ");
		hql.append(" com.scpii.universal.pns.entity.Users u  ");
		hql.append(" where u.id = t.createBy");
		hql.append(" and t.parentId='0' and t.targetId = " + tarId
				+ " order by t.id desc");

		Map<String, Object> map = new HashMap<String, Object>();
		// TODO map.put("appId", getAppId(enterId));
		map.put("appId", appId);
		list = commentsDao.findByHql(hql.toString(), pageNo, pageSize, null,
				null);
		if (list != null && list.size() > 0) {
			for (Comments comments : list) {
				comments.setCommentCount(getCircleInitByResIdCount(comments
						.getId()));

			}

		}

		return list;
	}

	public Integer getCircleInitCount(Integer appId, Long targetId) {

		// /List<ClientMoreDto> clentmoreList = moreContentService
		// .listMoreContentByCon(appId, GenericConstants.CIRCLE_MORE_TYPE);

		// StringBuffer condTip = new StringBuffer("(");
		// String condTipStr = "";
		List<Long> list = null;
		// if (clentmoreList != null && clentmoreList.size() > 0) {
		// appId升位
		Long tarId = targetId;
		if (!targetId.toString().startsWith("48")) {
			tarId = 400000000000L + targetId;
		}
		// for (ClientMoreDto moreDto : clentmoreList) {
		// condTip.append(targetId + moreDto.getINT_MORE_CONTENT() + ",");
		// }
		// condTipStr = condTip.substring(0, condTip.length() - 1);
		// condTipStr = condTipStr + ")";
		StringBuffer hql = new StringBuffer(
				"select count(*) from  com.scpii.universal.pns.entity.Comments t ,");
		hql.append(" com.scpii.universal.pns.entity.Users u  ");
		hql.append(" where u.id = t.createBy");
		hql.append(" and t.parentId='0' and t.targetId = " + tarId
				+ " order by t.id desc");

		Map<String, Object> map = new HashMap<String, Object>();
		// map.put("appId", getAppId(enterId)); 110000000001 TODO
		map.put("appId", appId);
		list = commentsDao.findByHqlCount(hql.toString(), 0, 0, null, null);

		if (list != null && list.size() == 1) {
			return list.get(0).intValue();
		} else {
			return 0;
		}

	}

	@Override
	public Integer getCircleInitByResIdCount(Long resId) {

		StringBuffer sql = new StringBuffer();
		sql.append("select {comm.*} from pns.COMMENTS comm");
		sql.append(" start with comm.PARENT_ID=:resId");
		sql.append(" connect by prior comm.ID=comm.PARENT_ID");
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("resId", resId);
		List<Comments> list = commentsDao.findBySql(sql.toString(), argsMap);
		return list.size();
	}

	@Override
	public List<Comments> getCircleByResIdInit(Long resId, Integer pageNo,
			Integer pageSize) {
		// TODO Auto-generated method stub

		StringBuffer sql = new StringBuffer();
		sql.append("select * from (select rownum rwn, {comm.*} from pns.COMMENTS comm");

		sql.append(" start with comm.PARENT_ID=:resId");
		sql.append(" connect by prior comm.ID=comm.PARENT_ID )");
		sql.append(" where rwn>" + (pageNo - 1) * pageSize + " and rwn<="
				+ pageNo * pageSize + "");

		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("resId", resId);
		List<Comments> list = commentsDao.findBySql(sql.toString(), argsMap);
		if (list != null && list.size() > 0) {
			for (Comments comm : list) {
				if (comm.getCreateBy() != null) {
					Users user = usersDao.get(comm.getCreateBy());
					if (user != null) {
						comm.setUserName(user.getUserAilas());
					}
				}
			}
		}

		return list;

	}

	@Override
	public void updateByResId(Long resId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select {comm.*} from pns.COMMENTS comm,pns.Users u");
		sql.append(" where comm.CREATE_BY=u.id");
		sql.append(" start with comm.id=:resId");
		sql.append(" connect by prior comm.ID=comm.PARENT_ID");
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("resId", resId);
		List<Comments> list = commentsDao.findBySql(sql.toString(), argsMap);
		if (list != null && list.size() > 0) {
			for (Comments comm : list) {
				commentsDao.delete(comm.getId());
				// delete uses_actions
				argsMap = new HashMap<String, Object>();
				argsMap.put("objectId", comm.getId());
				// Comments comments = getCommentsById(commentsId);

				try {
					userActionsDao.updateByHql(
							"DELETE_USERaCTIONS_BY_OBJECT_ID", argsMap);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

	@Override
	public List<ClientMoreDto> getCircleCateInit(Integer appId, Integer pageNo,
			Integer pageSize) {
		List<ClientMoreDto> clentmoreList = moreContentService
				.listMoreContentByCon(appId, GenericConstants.CIRCLE_MORE_TYPE);

		return clentmoreList;

	}

	@Override
	public Integer getCircleCateInitCount(Integer appId) {
		List<ClientMoreDto> clentmoreList = moreContentService
				.listMoreContentByCon(appId, GenericConstants.CIRCLE_MORE_TYPE);
		return clentmoreList.size();
	}

}
